﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyStep.Business.Interfaces;
using EasyStep.Data.Entities;
using EasyStep.Business.Providers;
using EasyStep.Business.Interfaces.Providers;

namespace EasyStep.Business.Managers
{
    public class TestProjectManager : BaseDataManager<ITestProjectProvider, TestProject>, ITestProjectManager
    {
        public TestProjectManager(ITestProjectProvider provider):base(provider)
        {                
        }

        public IEnumerable<TestProject> GetUserProjects(Guid userId)
        {
            return Provider.GetUserProjects(userId);
        }

        

        public TestProject GetOrCreateProject(string name, Guid userId, Func<TestProject> onNotFound = null)
        {   
         
            if (onNotFound == null)
                onNotFound = () => new TestProject() { Name = name, UserId = userId };

            TestProject result = null;
            //by id 
            Guid prId;
            if (Guid.TryParse(name, out prId) && !prId.Equals(Guid.Empty))
                result = Get(prId);
            if (result != null || string.IsNullOrEmpty(name)) return result;
            //by name
            result = GetOrCreateItemByName(name, () => GetUserProjects(userId), onNotFound);
            return result;
        }

        public TestProject Get(Guid id)
        {
            return Provider.Get(id);
        }
    }
}
